/**
 * @author Marc Went
 * @version 1.2.2
 * 
 * 
 *    Copyright 2012 Marc Went
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 * 
 */

package com.mwent.fullscreen;

import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;

public class Fullscreen {

	/**
	 * Show a JFrame fullscreen on the main computer screen
	 * @param frame
	 */
	public Fullscreen(JFrame frame){
		this(1, frame);
	}
	
	/**
	 * Show a Object extending JFrame fullscreen on the main computer screen
	 * Throws error if the insterted frame could not be cast to a JFrame
	 * @param frame
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public Fullscreen(Class<? extends JFrame> frame) throws InstantiationException, IllegalAccessException{
		this(1, (JFrame) frame.newInstance());
	}
	/**
	 * Show a Object extending JFrame fullscreen on the given screen
	 * Throws error if the insterted frame could not be cast to a JFrame
	 * @param screen from 1 to n
	 * @param an Object that extends a JFrame
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public Fullscreen(int screen, Class<? extends JFrame> frame) throws InstantiationException, IllegalAccessException{
		this(screen, (JFrame) frame.newInstance());
	}
	
	/**
	 * Show a JFrame fullscreen on the given screen
	 * @param screen from 1 to n
	 * @param a JFrame object
	 */
	public Fullscreen(int screen, JFrame frame){
		createFrame(screen, frame);

	}
	
	private void createFrame(int screen, JFrame frame) {
		Dimension screenDim = getScreenDimension(screen);
		if(frame.getDefaultCloseOperation() == JFrame.DO_NOTHING_ON_CLOSE)	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setUndecorated(true);
		frame.setSize(screenDim);
		frame.setIconImages(frame.getIconImages());
		showOnScreen(screen, frame);
		frame.setVisible(true);
		frame.setFocusableWindowState(false);
		
	}
	
	private void showOnScreen(int screen, JFrame frame)
	{
		screen -= 1;
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		GraphicsDevice[] gs = ge.getScreenDevices();
		int offsetX = 0;
		for(int i = 0; i < screen; i++){
			offsetX += gs[i].getDisplayMode().getWidth();
		}
		if( screen > -1 && screen < gs.length )
		{
			GraphicsConfiguration gcBound = gs[screen].getDefaultConfiguration();
			frame.setLocation(offsetX, gcBound.getBounds().y);
		}
		else if( gs.length > 0 )
		{
			gs[0].setFullScreenWindow( frame );
		}
		else
		{
			throw new RuntimeException( "No Screens Found" );
		}
	}
	private Dimension getScreenDimension(int screen) {
		screen -= 1;
		Dimension d;
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		GraphicsDevice[] gs = ge.getScreenDevices();

		if( screen > -1 && screen < gs.length )
		{
			DisplayMode dm = gs[screen].getDisplayMode();
			//GraphicsConfiguration gcBound = gs[screen].getDefaultConfiguration();
			d = new Dimension(dm.getWidth(), dm.getHeight());
		}
		else if( gs.length > 0 )
		{
			DisplayMode dm = gs[0].getDisplayMode();
			d = new Dimension(dm.getWidth(), dm.getHeight());
		}
		else
		{
			d = new Dimension();
		}
		return d;
	}
}
